import pymysql

# 创建与数据库的连接
db = pymysql.connect(
    host="192.168.122.203",
    user="root",
    password="root",
    database="testdb",
    port=3307,  # 指定端口号
)

# 创建游标对象cursor
cursor = db.cursor()

# 插入sql语句
sql = "delete from student where age < 22"

try:
    # 执行sql语句
    cursor.execute(sql)
    # 提交事务
    db.commit()
    print("删除数据成功")
except:
    db.rollback()
    print("删除数据失败")
finally:
    # 关闭连接
    db.close()
